# -- coding:UTF-8 --
from flask import Blueprint, request

from config.mysql_db import db
from service import user_service
from util import res_util
from vo.table_model import File

space_api = Blueprint("space", __name__, url_prefix='/api/space_api')



@space_api.route('/get_content', methods=['POST', "GET"])
def get_content():
    user_id = user_service.get_id_by_token()
    args = request.args.to_dict()
    vos = db.session.query(File) \
        .filter(File.user_id == user_id) \
        .filter(File.parent_id == args.get('parent_id', 0)).all()
    return res_util.success(vos)


@space_api.route('/create', methods=['POST', "GET"])
def create():
    request_body = request.json
    file = File()
    file.parent_id = request_body.get('parent_id', 0)
    file.user_id = user_service.get_id_by_token()
    file.path = request_body.get('path')
    file.is_public = request_body.get('is_public')
    file.is_file = request_body.get('is_file')
    file.name = request_body.get('name')
    db.session.add(file)
    db.session.commit()
    return res_util.success()

@space_api.route('/update', methods=['POST', "GET"])
def update():
    args = request.json
    db.session \
        .query(File) \
        .filter(File.user_id == user_service.get_id_by_token()) \
        .filter(File.id == args['id']) \
        .update(File.name == args['name'])
    db.session.commit()
    return res_util.success()

@space_api.route('/delete', methods=['POST', "GET"])
def delete():
    args = request.json
    db.session.query(File) \
        .filter(File.user_id == user_service.get_id_by_token()) \
        .filter(File.id == args['id']).delete()
    db.session.commit()
    return res_util.success()